package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.FragmentScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.MyTotalAsset;
import com.chainyoung.component_tmperson.entity.Purse;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletContact;

import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@FragmentScope
public class WalletPresenter extends BasePresenter<WalletContact.Model, WalletContact.View> {


    @Inject
    public WalletPresenter(WalletContact.Model model, WalletContact.View rootView) {
        super(model, rootView);
    }


    public void getMyPurse(TreeMap<String, Object> map) {
        addSubscribe(mModel.getMyPurse(map)
                .compose(RxUtils.<CommonList<Purse>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonList<Purse>>(mView) {
                    @Override
                    public void onNext(CommonList<Purse> data) {
                        mView.onGetMyPurseSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetMyPurseSuccess(null);
                                return;
                            }
                        }
                        mView.onGetMyPurseFailed();
                        super.onError(e);
                    }
                }));
    }

    public void getMyTotalAsset(TreeMap<String, Object> map) {
        addSubscribe(mModel.getMyTotalAsset(map)
                .compose(RxUtils.<MyTotalAsset>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<MyTotalAsset>(mView) {
                    @Override
                    public void onNext(MyTotalAsset data) {
                        mView.onGetTotalAsset(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetTotalAsset(null);
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }


}
